package config

import (
	"log"

	"github.com/spf13/viper"
)

var (
	v       = viper.New()
	GConfig = &Config{}
)

func InitGconfig(fileName, filePath string) {

	v.SetConfigName(fileName)
	v.AddConfigPath(filePath)

	// 找到并读取配置文件并且 处理错误读取配置文件
	if err := v.ReadInConfig(); err != nil {
		panic(err)
	}
	err := v.Unmarshal(GConfig)
	if err != nil {
		log.Println("初始化公共配置异常", err.Error())
	}
	return
}

type Config struct {
	Config    LocalConfig `mapstructure:"config"`
	Log       Log         `mapstructure:"log"`
	Transport Transport   `mapstructure:"transport"`
	Trace     Trace       `mapstructure:"trace"`
	Redis     []Redis     `mapstructure:"redis"`
	Consul    Consul      `mapstructure:"consul"`
	Nsq       Nsq         `mapstructure:"nsq"`
	Mysql     []Mysql     `mapstructure:"mysql"`
	OSS       OSS         `mapstructure:"oss"`
	Sms       Sms         `mapstructure:"sms"`
	JwtConfig Jwt         `mapstructure:"jwt"`
	Wechat    Wechat      `mapstructure:"wechat"`
}

type Transport struct {
	HTTP         HTTPConfig `mapstructure:"http"`
	InnerHTTP    HTTPConfig `mapstructure:"inner_http"`
	CmdInnerHTTP HTTPConfig `mapstructure:"cmd_inner_http"`
	Grpc         GrpcConfig `mapstructure:"grpc"`
}
type GrpcConfig struct {
	Addr string `mapstructure:"addr"`
}

type Wechat struct {
	AppId           string `mapstructure:"app_id" json:"app_id"`
	AppSecret       string `mapstructure:"app_secret" json:"app_secret"`
	Url             string `mapstructure:"url" json:"url"`
	Token           string `mapstructure:"token" json:"token"`
	MchId           string `mapstructure:"mch_id" json:"mch_id"`
	MchKey          string `mapstructure:"mch_key" json:"mch_key"`
	SubMchId        string `mapstructure:"sub_mch_id" json:"sub_mch_id"`
	SubMchName      string `mapstructure:"sub_mch_name" json:"sub_mch_name"`
	ProfitSharing   string `mapstructure:"profit_sharing" json:"profit_sharing"`
	NotifyUrl       string `mapstructure:"notifyUrl" json:"notifyUrl"`
	RefundNotifyUrl string `mapstructure:"refundNotifyUrl" json:"refundNotifyUrl"`

	ApiclientCert string `mapstructure:"api_client_cert" json:"api_client_cert"`
	ApiclientKey  string `mapstructure:"api_client_key" json:"api_client_key"`

	Port int `mapstructure:"port" json:"port"`
}

type HTTPConfig struct {
	Addr              string  `mapstructure:"addr"`
	MaxConns          int     `mapstructure:"max_conns"`
	ReadTimeout       float64 `mapstructure:"read_timeout"`
	ReadHeaderTimeout float64 `mapstructure:"read_header_timeout"`
	WriteTimeout      float64 `mapstructure:"write_timeout"`
	IdleTimeout       float64 `mapstructure:"idle_timeout"`
}

type LocalConfig struct {
	Model   string `mapstructure:"model"`
	ImgHost string `mapstructure:"imgHost"`
}
type Jwt struct {
	Secret  string `mapstructure:"secret"`
	Timeout int64  `mapstructure:"timeout"`
}

type Sms struct {
	AppKey    string `mapstructure:"appKey"`
	AppSecret string `mapstructure:"appSecret"`
}

type Log struct {
	App struct {
		AppFilePath string `mapstructure:"app_file_path"`
		FilePath    string `mapstructure:"file_path"`
		Level       string `mapstructure:"level"`
	} `mapstructure:"app"`

	Access struct {
		FilePath string `mapstructure:"file_path"`
	} `mapstructure:"access"`
}

type Trace struct {
	ServiceName string  `mapstructure:"service_name"`
	FilePath    string  `mapstructure:"file_path"`
	Sampling    float64 `mapstructure:"sampling"`
}

type Redis struct {
	Name         string  `mapstructure:"name"`
	Addr         string  `mapstructure:"addr"`
	Pwd          string  `mapstructure:"pwd"`
	Db           float64 `mapstructure:"db"`
	DialTimeout  float64 `mapstructure:"dial_timeout"`
	ReadTimeout  float64 `mapstructure:"read_timeout"`
	WriteTimeout float64 `mapstructure:"write_timeout"`
	PoolSize     float64 `mapstructure:"pool_size"`
	MinIdleConns float64 `mapstructure:"min_idle_conns"`
	MaxRetries   float64 `mapstructure:"max_retries"`
}

type Mysql struct {
	Name   string      `mapstructure:"name"`
	Master MysqlConfig `mapstructure:"master"`
	Slave  MysqlConfig `mapstructure:"slave"`
}

type MysqlConfig struct {
	Driver         string  `mapstructure:"driver"`
	DSN            string  `mapstructure:"dsn"`
	MaxOpenConns   int32   `mapstructure:"max_open_conns"`
	MaxIdleConns   int32   `mapstructure:"max_idle_conns"`
	MaxLifeTimeout float64 `mapstructure:"max_life_timeout"`
}

type Consul struct {
	Kv Kv `mapstructure:"kv"`
}
type Kv struct {
	Url       string `mapstructure:"url"`
	KeyPerfix string `mapstructure:"key_perfix"`
}

type Server struct {
	Name string `mapstructure:"name"`
	Url  string `mapstructure:"url"`
}

type Nsq struct {
	Producer []Producer `mapstructure:"producer"`
	Consumer []Consumer `mapstructure:"consumer"`
}

type Producer struct {
	Name           string `mapstructure:"name"`
	Addr           string `mapstructure:"addr"`
	MaxConcurrency int32  `mapstructure:"max_concurrency"`
	DialTimeout    int32  `mapstructure:"dial_timeout"`
	ReadTimeout    int32  `mapstructure:"read_timeout"`
	WriteTimeout   int32  `mapstructure:"write_timeout"`
}

type Consumer struct {
	Name         string   `mapstructure:"name"`
	Addr         string   `mapstructure:"addr"`
	Lookup       []string `mapstructure:"lookup"`
	MaxInFlight  int32    `mapstructure:"max_inflight"`
	DialTimeout  int32    `mapstructure:"dial_timeout"`
	ReadTimeout  int32    `mapstructure:"read_timeout"`
	WriteTimeout int32    `mapstructure:"write_timeout"`
}

type OSS struct {
	AK       string `mapstructure:"ak"`
	AS       string `mapstructure:"as"`
	Endpoint string `mapstructure:"endpoint"`
}
